package com.ws.controller;

import com.ws.common.PaasPage;
import com.ws.common.PaasResponse;
import com.ws.entity.Department;
import com.ws.response.DepartmentListResponse;
import com.ws.response.DepartmentPageResponse;
import com.ws.response.DepartmentResponse;
import com.ws.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 部门管理
 * @author ：ws
 * @date ：Created in 2023/10/22 21:59
 * @description：TODO
 * @modified By：
 * @version: 1.0.0
 */
@Controller
@RefreshScope
@CrossOrigin
@RequestMapping("/depart")
public class DepartmentController {
    @Autowired
    DepartmentService departmentService;

    /**
     * 部门分页查询
     * @param pageable
     * @param departmentName
     * @param statusCd
     * @return
     */
    @GetMapping("")
    public ResponseEntity<DepartmentPageResponse> getDepartmentPage(
            @PageableDefault(page = 1, direction = Sort.Direction.DESC) Pageable pageable,
            @RequestParam(value="departmentName",required=false) String departmentName,
            @RequestParam(value="statusCd",required=false) String statusCd
            ){
        PaasPage<Department> departmentPaasPage = departmentService.getDepartmentPage(pageable,departmentName,statusCd);
        return ResponseEntity.ok(new DepartmentPageResponse(departmentPaasPage));
    }

    /**
     * 部门查询
     * @param departmentName
     * @param statusCd
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<DepartmentListResponse> getDepartmentList(
            @RequestParam(value="departmentName",required=false) String departmentName,
            @RequestParam(value="statusCd",required=false) String statusCd
    ){
        List<Department> departmentList = departmentService.getDepartmentList(departmentName,statusCd);
        return ResponseEntity.ok(new DepartmentListResponse(departmentList));
    }

    /**
     * 部门新增
     * @param department
     * @return
     */
    @PostMapping("")
    public ResponseEntity<DepartmentResponse> saveDepartment(@RequestBody @Valid Department department){
        Department department1 = departmentService.saveDepartment(department);
        return ResponseEntity.ok(new DepartmentResponse(department1));
    }

    /**
     * 查询个别部门信息
     * @param departmentId
     * @return
     */
    @GetMapping("/{departmentId}/basicInfo")
    public ResponseEntity<DepartmentResponse> getDepartmentBasicInfo(@PathVariable Long departmentId){
        Department department = departmentService.getDepartmentBasicInfo(departmentId);
        return ResponseEntity.ok(new DepartmentResponse(department));
    }

    /**
     * 删除对应的部门
     * @param departmentId
     * @return
     */
    @PostMapping("/d/{departmentId}")
    public ResponseEntity delUser(@PathVariable Long departmentId)  {
        departmentService.delDepartment(departmentId);
        return ResponseEntity.ok(new PaasResponse());
    }

    /**
     * 修改对应的部门
     * @param departmentId
     * @return
     */
    @PostMapping("/u/{departmentId}")
    public ResponseEntity<DepartmentResponse> updateDepartment(@PathVariable Long departmentId, @RequestBody Department department)  {
        Department department1 = departmentService.updateDepartment(departmentId,department);
        return ResponseEntity.ok(new DepartmentResponse(department1));
    }


}
